/*
 * Copyright (C) 2025 Huawei Device Co., Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef NODE_HTTP2_MAP_H
#define NODE_HTTP2_MAP_H
#include "http2_struct.h"
#include <js_native_api_types.h>
#include <map>
#include <shared_mutex>
#include <unordered_map>

template <typename K, typename V> class ConcurrentHashMap {
private:
    std::unordered_map<K, V> map_;
    mutable std::mutex mutex_;

public:
    void Insert(const K &key, const V &value)
    {
        std::lock_guard<std::mutex> lock(mutex_);
        map_[key] = value;
    }
    
    template<typename InputIt>
    void Insert(InputIt first, InputIt last)
    {
        std::lock_guard<std::mutex> lock(mutex_);
        map_.insert(first, last);
    }

    auto Find(const K &key)
    {
        std::lock_guard<std::mutex> lock(mutex_);
        return map_.find(key);
    }

    auto End()
    {
        std::lock_guard<std::mutex> lock(mutex_);
        return map_.end();
    }

    size_t Erase(const K &key)
    {
        std::lock_guard<std::mutex> lock(mutex_);
        return map_.erase(key);
    }

    V &operator[](const K &key)
    {
        std::lock_guard<std::mutex> lock(mutex_);
        return map_[key];
    }

    size_t Size() const
    {
        std::lock_guard<std::mutex> lock(mutex_);
        return map_.size();
    }
    
    void Clear()
    {
        std::lock_guard<std::mutex> lock(mutex_);
        map_.clear();
    }
    
    template<typename Func>
    void ForEach(Func&& func) const
    {
        std::lock_guard<std::mutex> lock(mutex_);
        for (const auto& pair : map_) {
            func(pair.first, pair.second);
        }
    }
};

template <typename K, typename V>
class ConcurrentMultiHashMap {
private:
    std::multimap<K, V> map_;
    mutable std::mutex mutex_;

public:
    
    void Insert(const K &key, const V &value)
    {
        std::lock_guard<std::mutex> lock(mutex_);
        map_.insert({key, value});
    }
    
    auto Find(const K &key)
    {
        std::lock_guard<std::mutex> lock(mutex_);
        return map_.find(key);
    }
    
    auto EqualRange(const K &key)
    {
        std::lock_guard<std::mutex> lock(mutex_);
        return map_.equal_range(key);
    }
    
    size_t Erase(const K &key)
    {
        std::lock_guard<std::mutex> lock(mutex_);
        return map_.erase(key);
    }
    
    auto End()
    {
        std::lock_guard<std::mutex> lock(mutex_);
        return map_.end();
    }
};

ConcurrentHashMap<string, Connection *> g_connectManagerMap;
ConcurrentHashMap<string, RequestTask *> g_requestManagerMap;
ConcurrentMultiHashMap<string, unordered_map<string, string>> g_responseHeadersManagerMap;
ConcurrentHashMap<string, int> g_sessionManagerMap;
ConcurrentHashMap<string, bool> g_streamCloseManagerMap;
ConcurrentHashMap<string, bool> g_freeManagerMap;
ConcurrentHashMap<string, napi_ref> g_callbackManagerMap;
ConcurrentHashMap<string, napi_env> g_envManagerMap;
ConcurrentHashMap<string, bool> g_callJsCheckDataManagerMap;
std::mutex g_responseHeadersMapMutex;
#endif // NODE_HTTP2_MAP_H
